package org.micode.common.helper;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Slf4j
public class MailHelper {

    public static void sendMail(String mailHost,
                                String mailPort,
                                String mailProtocol,
                                String isAuth,
                                String isSsl,
                                String mailFrom,
                                String mailFromPassword,
                                String mailTo,
                                String mailTitle,
                                String mailText)
            throws MessagingException {
        if (StringUtils.isAnyEmpty(mailHost) ||
                StringUtils.isAnyEmpty(mailPort) ||
                StringUtils.isAnyEmpty(mailProtocol) ||
                StringUtils.isAnyEmpty(isAuth) ||
                StringUtils.isAnyEmpty(isSsl) ||
                StringUtils.isAnyEmpty(mailFrom) ||
                StringUtils.isAnyEmpty(mailFromPassword) ||
                StringUtils.isAnyEmpty(mailTo) ||
                StringUtils.isAnyEmpty(mailTitle) ||
                StringUtils.isAnyEmpty(mailText)) {
            log.warn("There is a problem with the mail related configuration, or the mail content is empty");
            return;
        }

        Properties prop = new Properties();
        prop.setProperty("mail.smtp.host", mailHost);
        prop.setProperty("mail.smtp.auth", isAuth);
        prop.setProperty("mail.smtp.ssl.enable", isSsl);
        prop.setProperty("mail.transport.protocol", mailProtocol);
        prop.setProperty("mail.smtp.port", mailPort);

        // 使用JavaMail发送邮件的5个步骤

        // 1、创建session
        Session session = Session.getInstance(prop);
        // 开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
        // session.setDebug(true);

        // 2、通过session得到transport对象
        Transport ts = session.getTransport();
        try {
            // 3、使用邮箱的用户名和密码连上邮件服务器，发送邮件时，发件人需要提交邮箱的用户名和密码给smtp服务器，用户名和密码都通过验证之后才能够正常发送邮件给收件人。
            ts.connect(mailHost, mailFrom, mailFromPassword);

            // 4、创建邮件
            Message message = createSimpleMail(session, mailFrom, mailTo, mailTitle, mailText);

            // 5、发送邮件
            ts.sendMessage(message, message.getAllRecipients());
        } finally {
            ts.close();
        }
    }

    /**
     * 创建一封只包含文本的邮件
     */
    private static MimeMessage createSimpleMail(Session session,
                                               String mailFrom,
                                               String mailTo,
                                               String mailTitle,
                                               String mailText)
            throws MessagingException {
        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);

        // 指明邮件的发件人
        message.setFrom(new InternetAddress(mailFrom));

        // 指明邮件的收件人
        Address[] internetAddressTo = InternetAddress.parse(mailTo);
        message.setRecipients(Message.RecipientType.TO, internetAddressTo);

        // 邮件的标题
        message.setSubject(mailTitle);

        // 邮件的文本内容, 替换了换行符，收到的邮件网页显示就换行了
        mailText = mailText.replaceAll("\n", "<br>");
        message.setContent(mailText, "text/html;charset=UTF-8");

        // 返回创建好的邮件对象
        return message;
    }

    public static void main(String[] args) {
        String mailHost = "smtp.weizhizs.com";
        String mailPort = "465";
        String mailProtocol = "smtp";
        String isAuth = "true";
        String isSsl = "true";
        String mailFrom = "service@weizhizs.com";
        String mailFromPassword = "wzzsCRM001";
        String mailTo = "924241281@qq.com,wutianbin@weizhizs.com";
        String mailTitle = "抱歉，来问题了!";
        String mailText = "系统运行出异常，请及时处理";

        try {
            sendMail(mailHost,
                    mailPort,
                    mailProtocol,
                    isAuth,
                    isSsl,
                    mailFrom,
                    mailFromPassword,
                    mailTo,
                    mailTitle,
                    mailText);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
